﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Linq;
using FuelTracker.Models;
using Caliburn.Micro;
using FuelTracker.Services;
using System.Windows.Navigation;
using FuelTracker.ViewModels;
using FuelTracker.Resources;
using System.Threading;
using Microsoft.Phone.Controls;
using System.Windows.Threading;


namespace FuelTracker
{
    public class MainViewModel : Screen
    {
        private readonly INavigationService navigationService;
        private readonly IStorageService storageService;
        private readonly ParameterService parameterService;
        private Dispatcher dispatcher;

        private bool isLoading = false;
        public bool IsLoading
        {
            get { return isLoading; }
            set
            {
                isLoading = value;
                NotifyOfPropertyChange(() => IsLoading);
            }
        }

        private Vehicle lastVehicle;

        private Vehicle selectedVehicle;
        public Vehicle SelectedVehicle
        {
            get { return selectedVehicle; }
            set
            {
                if (selectedVehicle != value)
                {
                    selectedVehicle = value;
                    NotifyOfPropertyChange(() => SelectedVehicle);
                }
            }
        }

        private ObservableCollection<FuelLogViewModel> lastFuelLogs = new ObservableCollection<FuelLogViewModel>();
        public ObservableCollection<FuelLogViewModel> LastFuelLogs
        {
            get { return lastFuelLogs;  }
        }

        private ObservableCollection<Vehicle> vehicles = new ObservableCollection<Vehicle>();
        public ObservableCollection<Vehicle> Vehicles
        {
            get { return vehicles;  }
        }


        public MainViewModel(IStorageService storageService,
            INavigationService navigationService,
            ParameterService parameterService)
        {
            Debug.WriteLine("Konstruktor MainViewModel");
            this.navigationService = navigationService;
            this.storageService = storageService;
            this.parameterService = parameterService;
        }


        private void BeginLoadVehicles()
        {
            //IsLoading = true;
            //Debug.WriteLine("BeginLoadVehicle on Thread " + Thread.CurrentThread.ManagedThreadId);

            //ThreadPool.QueueUserWorkItem(c =>
            //    {
            //        Thread.Sleep(5000);

            //        var vehicles = storageService.Vehicles.ToList();
            //        Vehicle vehicle = vehicles.FirstOrDefault();
            //        Debug.WriteLine("LoadVehicle on Thread " + Thread.CurrentThread.ManagedThreadId);
            //        dispatcher.BeginInvoke(new Action<List<Vehicle>>(EndLoadVehicle), vehicles );
            //    });
        }

        private void EndLoadVehicle(List<Vehicle> vehicles)
        {
            //Debug.WriteLine("EndLoadVehicle on Thread " + Thread.CurrentThread.ManagedThreadId);

            //foreach (Vehicle vehicle in vehicles)
            //{
            //    this.vehicles.Add(vehicle);
            //}
            //lastVehicle = vehicles.FirstOrDefault();

            //if (lastVehicle != null)
            //{
            //    foreach (FuelLog fuelLog in lastVehicle.FuelLogs.Take(15))
            //    {
            //        lastFuelLogs.Add(new FuelLogViewModel(fuelLog));
            //    }
            //}

            //IsLoading = false;
        }
         
        public void CreateVehicle()
        {
            navigationService.Navigate(Uris.CreateVehicleView);
        }

        protected override void OnActivate()
        {
            Debug.WriteLine("Activate MainViewModel");

            base.OnActivate();

            SelectedVehicle = null;

            if (parameterService.HasEntries)
            {
                Vehicle newVehicle = (Vehicle)parameterService.Pop();
                vehicles.Add(newVehicle);
            }
        }

        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);
            Debug.WriteLine("OnViewLoaded MainViewModel");

            PhoneApplicationPage page = view as PhoneApplicationPage;
            dispatcher = page.Dispatcher;

            //BeginLoadVehicles();
        }

        public void SelectVehicle()
        {
            if (selectedVehicle == null)
                return;

            Debug.WriteLine(selectedVehicle.Name + " ausgewählt");
            parameterService.Push(selectedVehicle);
            navigationService.Navigate(Uris.FuelLogsView(selectedVehicle.Name));
        }

        #region Lokalization

        public String LabelApplicationTitle
        {
            get { return Texts.Lbl_ApplicationTitle; }
        }

        public String LabelRecent
        {
            get { return Texts.Lbl_Recent; }
        }

        public String LabelVehicles
        {
            get { return Texts.Lbl_Vehicles; }
        }

        public String LabelLoading
        {
            get { return Texts.Loading; }
        }

        #endregion
    }
}